<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <!-- <div class="filter-container"> -->
    <el-row>

      <el-input
        clearable
        v-model="keystr"
        class="filter-item"
        style="width: 200px;marginRight:12px"
        placeholder="姓名/登录名 或 联系电话"
      />

      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        style="marginRight:12px;"
        @click="netUserlist"
      >搜索</el-button>

      <el-button
        class="filter-item"
        type="text"
        @click="addBtnClickHandle"
      >新增</el-button>
    </el-row>

    <!-- </div> -->
    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
      style="marginTop:12px;"
    >
      <el-table-column
        align="center"
        width="100px"
        label="序号"
        prop="id"
        sortable
      />

      <el-table-column
        align="center"
        label="姓名/登录名"
        prop="account"
      />
      <el-table-column
        align="center"
        label="联系电话"
        prop="mobile"
      />
      <el-table-column
        align="center"
        label="负责项目"
        prop="project"
      />

      <el-table-column
        align="center"
        label="操作"
        width="250"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="editBtnClickHandle(scope.row)"
          >编辑</el-button>
          <el-button
            type="danger"
            size="mini"
            @click="delBtnClickHandle(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      @pagination="netUserlist"
    />
    <!-- 用户编辑对话框 -->
    <el-dialog
      :visible.sync="showEditAlert"
      :title="currentUser.id==undefined?'新增用户信息':'编辑用户信息'"
      width="40%"
    >
      <el-form
        :model="currentUser"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left: 50px"
      >
        <el-form-item
          label="姓名/登录名"
          prop="account"
        >
          <el-input v-model="currentUser.account" />
        </el-form-item>
        <el-form-item
          label="联系电话"
          prop="mobile"
        >
          <el-input v-model="currentUser.mobile" />
        </el-form-item>
        <el-form-item
          label="密 码"
          prop="password"
        >
          <el-input v-model="currentUser.password" />
        </el-form-item>
        <el-form-item
          label="设置项目"
          prop="project"
        >
          <el-select
            v-model="selectedProjectList"
            multiple
            placeholder="请选择"
            style="width:100%;"
          >
            <el-option
              v-for="(obj,idx) in projectList"
              :key="idx"
              :value="obj.base_id"
              :label="obj.project_name"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="doneUserInfo"
        >确定</el-button>
        <el-button
          type="default"
          @click="showEditAlert = false"
        >取消</el-button>
      </div>
    </el-dialog>

    <!-- 删除对话框 -->
    <el-dialog
      title="确认删除"
      :visible.sync="showDelAlert"
      width="20%"
    >
      <span>确认删除 {{currentUser.account}} 用户信息</span>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="default"
          @click="showDelAlert = false"
        >取消</el-button>
        <el-button
          type="danger"
          @click="doneDelHandle"
        >删除</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  userlist,
  queryProjects,
  saveSysUser,
  deleteSysUser,
  modifySysUser,
  getById,
} from "@/api/user";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import ProjectListVue from "../project/ProjectList.vue";

export default {
  name: "User",
  components: { Pagination },
  data() {
    return {
      keystr: undefined,
      listLoading: false,
      pageNum: 1,
      pageSize: 30,
      total: 0,
      list: [
        //  { "company_id": "", "department_id": "", "company_name": "", "department_name": "", "name": "测试", "mobile": "17756986985", "description": "", "project": "项目二", "account": "测试" }
      ],
      showEditAlert: false,
      showDelAlert: false,

      currentUser: {},
      projectList: [],
      selectedProjectList: [],
    };
  },
  created() {
    this.netUserlist();
    this.netQueryProjects();
  },
  mounted() {},
  methods: {
    //新增
    addBtnClickHandle() {
      this.currentUser = {};
      this.selectedProjectList = [];
      this.showEditAlert = true;
    },
    //编辑
    editBtnClickHandle(data) {
      this.netReadUserInfo(data["id"]);
      //   this.currentUser = data;
      this.showEditAlert = true;
    },
    //删除
    delBtnClickHandle(data) {
      this.currentUser = data;
      this.showDelAlert = true;
    },

    doneUserInfo() {
      let userProjectList = this.selectedProjectList.map((e) => ({
        baseId: e,
      }));
      this.currentUser.userProjectList = userProjectList;
      let self = this;

      if (this.currentUser.id == undefined) {
        saveSysUser(this.currentUser)
          .then((response) => {
            self.$message.success("添加成功");
            self.showEditAlert = false;
            self.netUserlist();
          })
          .catch(() => {});
      } else {
        modifySysUser(this.currentUser)
          .then((response) => {
            self.$message.success("修改成功");
            self.showEditAlert = false;
            self.netUserlist();
          })
          .catch(() => {});
      }
    },

    doneDelHandle() {
      let self = this;
      deleteSysUser(this.currentUser)
        .then((response) => {
          self.$message.success("删除成功");
          self.showDelAlert = false;
          self.netUserlist();
        })
        .catch(() => {});
    },

    /// 查询项目列表
    netQueryProjects() {
      let self = this;
      queryProjects({})
        .then((response) => {
          self.projectList = response["list"];
        })
        .catch(() => {});
    },

    netUserlist() {
      let self = this;
      this.listLoading = true;
      userlist({
        name: "",
        mobile: "",
        name: this.keystr,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      })
        .then((response) => {
          let { list, total } = response;
          self.list = list;
          self.listLoading = false;
          self.total = total;
        })
        .catch(() => {
          self.list = [];
          self.total = 0;
          self.listLoading = false;
        });
    },
    netReadUserInfo(uid) {
      let self = this;
      getById(uid).then((response) => {
        self.currentUser = response;
        let project = response["project"];
        self.selectedProjectList = project.map((item) => item["base_id"]);
        self.currentUser.id = uid;
      });
    },
  },
};
</script>
